package com.hzw.saas.service.old.storage.service;

import java.util.Date;
import java.util.Optional;
import javax.annotation.Resource;

import com.hzw.saas.api.old.storage.IResourceFileService;
import com.hzw.saas.api.old.storage.IStorageService;
import com.hzw.saas.api.old.storage.bo.StorageCapacityBO;
import com.hzw.saas.api.old.storage.enums.FileIntegrityEnum;
import com.hzw.saas.api.old.storage.enums.ResourceStatusEnum;
import com.hzw.saas.api.user.IUserRightsService;
import com.hzw.saas.api.user.bo.UserRightsBO;
import com.hzw.saas.api.workspace.IWorkspaceResourceService;
import com.hzw.saas.common.util.UnitConvertUtils;
import com.hzw.saas.common.util.enums.StorageUnitEnum;
import com.hzw.saas.service.storage.config.StorageConfig;
import cn.hutool.core.util.StrUtil;
import org.springframework.stereotype.Service;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;


/**
 * @author zzl
 * @since 05/25/2021
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class OldStorageServiceImpl implements IStorageService {

    // 默认有效小数位
    private static final int DEFAULT_SCALE = 2;

    private final StorageConfig storageConfig;

    @Resource
    private IUserRightsService userRightsService;
    @Resource
    private IResourceFileService psFileService;
    @Resource
    private IWorkspaceResourceService wsFileService;

    @Override
    public StorageCapacityBO getStorageCapacity(String userId) {
        StorageCapacityBO result = new StorageCapacityBO();
        if (StrUtil.isBlank(userId)) return result;
        // 总容量
        Optional<UserRightsBO> userRights = Optional.ofNullable(userRightsService.getUserRightsByUser(userId));
        String cloudStorage = userRights.map(UserRightsBO::getCloudStorageSize).orElse("");
        long storageByteSize = UnitConvertUtils.convertStorage(cloudStorage, StorageUnitEnum.BYTE).longValue();
        result.setTotalSize(storageByteSize);
        // 过期时间
        Date storageExpireTime = userRights.map(UserRightsBO::getCloudStorageExpireTime).orElse(null);
        result.setStorageExpireTime(storageExpireTime);
        // 总使用容量
        long totalUsedSize = 0L;
        // 个人空间使用量
        long psUsedSize = psFileService.totalSize(userId, storageConfig.getPersonalSpaceCatalog(), ResourceStatusEnum.NORMAL.code(), FileIntegrityEnum.NORMAL.code());
        totalUsedSize += psUsedSize;
        result.setPsUsedSize(psUsedSize);
        // 工作空间使用量
        long wsUsedSize = wsFileService.getFileSize(userId, "/");
        totalUsedSize += wsUsedSize;
        result.setWsUsedSize(wsUsedSize);
        result.setUsedSize(totalUsedSize);
        // size text
        this.setText(result);
        return result;
    }

    private void setText(StorageCapacityBO capacity) {
        long remainSize = capacity.getTotalSize() - capacity.getUsedSize();
        if (remainSize < 0)
            remainSize = 0;
        capacity.setTotalSizeText(UnitConvertUtils.suitableStorageText(capacity.getTotalSize(), DEFAULT_SCALE));
        capacity.setUsedSizeText(UnitConvertUtils.suitableStorageText(capacity.getUsedSize(), DEFAULT_SCALE));
        capacity.setRemainSizeText(UnitConvertUtils.suitableStorageText(remainSize, DEFAULT_SCALE));
        capacity.setPsTotalSizeText(UnitConvertUtils.suitableStorageText(capacity.getPsTotalSize(), DEFAULT_SCALE));
        capacity.setPsUsedSizeText(UnitConvertUtils.suitableStorageText(capacity.getPsUsedSize(), DEFAULT_SCALE));
        capacity.setWsTotalSizeText(UnitConvertUtils.suitableStorageText(capacity.getWsTotalSize(), DEFAULT_SCALE));
        capacity.setWsUsedSizeText(UnitConvertUtils.suitableStorageText(capacity.getWsUsedSize(), DEFAULT_SCALE));
    }
}
